<template>
  <van-address-edit :area-list="areaList" :address-info="addressInfo" show-postal show-delete show-set-default
    show-search-result :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave"
    @delete="onDelete" @change-detail="onChangeDetail" />

</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Toast } from 'vant';
import { areaList } from "@vant/area-data";
import { useAddress } from "../../store/user"
import { AddressType } from "../../types/zzy_types/user"
import { useRoute } from 'vue-router';
const route = useRoute()

// const addressInfo = ref<AddressType>();
const addressInfo = ref(
  {
    name: '周洲',
    tel: '15310235607',
    postalCode: '652188',
    addressDetail: '清华大学',
    areaCode: "500107",
    isDefault: true,
  }
)
const address = useAddress()
const searchResult = ref([]);
let idx = Number(route.params.idx);
console.log(idx)
if (idx) {
  addressInfo.value = address.list[idx]
}
const onSave = (content: AddressType) => {
  //新增
  let idx = Number(route.params.idx);
  console.log(idx)
  if (idx == -1) {
    address.addressAddAction(content)
    console.log("保存", idx, content)
    // return
  } else {
    console.log("更新", address.list[idx].objectId, content)
    address.addressUpdAction(address.list[idx].objectId, content)
  }
  //编辑更新

}
//删除
const onDelete = (content: AddressType) => {
  if (content) {
    address.addressDelAction(address.list[idx].objectId)
  }
}
const onChangeDetail = (val: AddressType) => {
  if (val) {
    searchResult.value = [
      // {
      //   name: '黄龙万科中心',
      //   address: '杭州市西湖区',
      // },
    ];
  } else {
    searchResult.value = [];
  }
};
</script>

<style lang="scss" scoped>

</style>